/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: Universidad de Almeria
 * License Type: Purchased
 */
package foroindaltronia.bd;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;

public class ModeradorDAO {
	public static Moderador loadModeradorByORMID(String usuario) throws PersistentException  {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return loadModeradorByORMID(session, usuario);
	}
	
	public static Moderador loadModeradorByORMID(PersistentSession session, String usuario) throws PersistentException {
		return (Moderador) session.load(foroindaltronia.bd.Moderador.class, usuario);
	}
	
	public static Moderador[] listModeradorByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return listModeradorByQuery(session, condition, orderBy);
	}
	
	public static Moderador[] listModeradorByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From foroindaltronia.bd.Moderador as Moderador");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			List list = query.list();
			return (Moderador[]) list.toArray(new Moderador[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Moderador loadModeradorByQuery(String condition, String orderBy) throws PersistentException {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return loadModeradorByQuery(session, condition, orderBy);
	}
	
	public static Moderador loadModeradorByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Moderador[] moderadors = listModeradorByQuery(session, condition, orderBy);
		if (moderadors != null && moderadors.length > 0)
			return moderadors[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateModeradorByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return iterateModeradorByQuery(session, condition, orderBy);
	}
	
	public static java.util.Iterator iterateModeradorByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From foroindaltronia.bd.Moderador as Moderador");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Moderador createModerador() {
		return new foroindaltronia.bd.Moderador();
	}
	
	public static boolean save(foroindaltronia.bd.Moderador moderador) {
		try {
			foroindaltronia.bd.ProyectoForoPersistentManager.instance().saveObject(moderador);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean delete(foroindaltronia.bd.Moderador moderador) {
		try {
			foroindaltronia.bd.ProyectoForoPersistentManager.instance().deleteObject(moderador);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteAndDissociate(foroindaltronia.bd.Moderador moderador) {
		try {
			foroindaltronia.bd.Tema[] lGestionas = moderador.gestiona.toArray();
			for(int i = 0; i < lGestionas.length; i++) {
				lGestionas[i].setGestionado(null);
			}
			return delete(moderador);
		}
		catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteAndDissociate(foroindaltronia.bd.Moderador moderador, org.orm.PersistentSession session) {
		try {
			foroindaltronia.bd.Tema[] lGestionas = moderador.gestiona.toArray();
			for(int i = 0; i < lGestionas.length; i++) {
				lGestionas[i].setGestionado(null);
			}
			try {
				session.delete(moderador);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean refresh(foroindaltronia.bd.Moderador moderador) {
		try {
			foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession().refresh(moderador);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static Moderador loadModeradorByCriteria(ModeradorCriteria moderadorCriteria) {
		Moderador[] moderadors = listModeradorByCriteria(moderadorCriteria);
		if(moderadors == null || moderadors.length == 0) {
			return null;
		}
		return moderadors[0];
	}
	
	public static Moderador[] listModeradorByCriteria(ModeradorCriteria moderadorCriteria) {
		return moderadorCriteria.listModerador();
	}
}
